package com.itheima.service.store.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.domain.store.Company;
import com.itheima.factory.MapperFactory;
import com.itheima.mapper.store.CompanyMapper;
import com.itheima.service.store.CompanyService;
import com.itheima.utils.TransactionUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;
import java.util.UUID;

public class CompanyServiceImpl implements CompanyService {
    @Override
    public Page<Company> findAll(String pageNum,String pageSize) {
        SqlSession sqlSession = MapperFactory.getSqlSession();
        //获取映射对象
        CompanyMapper mapper = MapperFactory.getMapper(sqlSession, CompanyMapper.class);
        //配置分页信息
        int num = 1;
        int size = 10;
        if(pageNum!=null){
            num = Integer.parseInt(pageNum);
        }
        if(pageSize!=null){
            size = Integer.parseInt(pageSize);
        }
        //配置分页
        Page<Company> page = PageHelper.startPage(num, size);
        //获取用户列表
        mapper.findAll();
        //释放资源
        TransactionUtil.close(sqlSession);
        //返回
        return page;
    }

    @Override
    public List<Company> findAll() {
        SqlSession sqlSession = MapperFactory.getSqlSession();
        //获取映射对象
        CompanyMapper mapper = MapperFactory.getMapper(sqlSession, CompanyMapper.class);
        //获取用户列表
        List<Company> list = mapper.findAll();
        //释放资源
        TransactionUtil.close(sqlSession);
        //返回
        return list;
    }

    @Override
    //添加企业
    public void addCompany(Company company) {
        SqlSession sqlSession = MapperFactory.getSqlSession();
        //获取映射对象
        CompanyMapper mapper = MapperFactory.getMapper(sqlSession, CompanyMapper.class);
        //主键id处理
        String id = UUID.randomUUID().toString().replaceAll("-", "");
        company.setId(id);
        //添加用户
        mapper.addCompany(company);
        //提交事务
        TransactionUtil.commit(sqlSession);
        //释放资源
        TransactionUtil.close(sqlSession);
    }

    @Override
    //通过ids删除
    public boolean deleteByIds(String ids) {
        try{
            SqlSession sqlSession = MapperFactory.getSqlSession();
            //获取映射对象
            CompanyMapper mapper = MapperFactory.getMapper(sqlSession, CompanyMapper.class);
            //ids处理
            String[] idArr = ids.split(",");
            //添加用户
            mapper.deleteByIds(idArr);
            //提交事务
            TransactionUtil.commit(sqlSession);
            //释放资源
            TransactionUtil.close(sqlSession);

        }catch (Exception e){
            return false;
        }

        return true;
    }

    @Override
    public Company findById(String id) {
        SqlSession sqlSession = MapperFactory.getSqlSession();
        //获取映射对象
        CompanyMapper mapper = MapperFactory.getMapper(sqlSession, CompanyMapper.class);
        //查找用户
        Company com = mapper.findById(id);
        //释放资源
        TransactionUtil.close(sqlSession);
        return com;
    }

    @Override
    public void updateCompany(Company company) {
        SqlSession sqlSession = MapperFactory.getSqlSession();
        //获取映射对象
        CompanyMapper mapper = MapperFactory.getMapper(sqlSession, CompanyMapper.class);
        //更新信息
        mapper.updateCompany(company);
        //提交事务
        TransactionUtil.commit(sqlSession);
        //释放资源
        TransactionUtil.close(sqlSession);
    }
}
